﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.EmailUtility;
using Ivy.Common.Framework.GeneralUtility;

using Ivy.UserManagement.Entity;
using Ivy.UserManagement.DataAccess;
using Ivy.UserManagement.IBusinessLogic;

using Ivy.SiteAdministration.Entity;
using Ivy.SiteAdministration.DataAccess;
using Ivy.SiteAdministration.IBusinessLogic;

namespace Ivy.UserManagement.BusinessLogic
{
    public class UserManagementBus : IUserManagementBus
    {
        #region  Variable & constant
        // All constants and local variable should be declared here
        // TRY NOT TO CREATE PUBLIC VARIABLES INSTEAD USE PROPERTIES FOR THAT PURPOSE.
        // Variable naming convention :- 
        // The naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Naming convention for all local variable should start with underscore "_"
        // Naming convention for all local constant should start with "_The"
        // Example :    string _CustomerCode = string.Empty;
        // Example :    const string _TheDefaultSmtpServer = "DefaultSmtpServer";
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion  Variable & constant

        #region Constructor & Destructor
        // Cut and Copy the default constructor for the class inside this region.
        // Generally not required in UI layer.
        // Example:
        // public SampleClassTemplate()
        // {

        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Constructor & Destructor

        #region Properties
        // Any value/variable that needs to be exposed outside the class we need to create a property.
        // Avoid creating public variables rather create public properties.
        // UI layer may have public custom properties. these are very handy for User Controls.
        // Property naming convention :- Properties will be exposed outside the class, 
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // All properties should have a corresponding local variable 
        // and the property name and local variable name should match.
        // Example :
        // public string CustomerCode
        // {
        //     get
        //     {
        //         return _CustomerCode;
        //     }
        //     set
        //     {
        //         _CustomerCode = value;
        //     }
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Properties

        #region Events
        // Generally UI layer will have public events.
        // Cut and Copy the default generated events for the class inside this region.
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Events

        #region Public Methods
        // Any method that needs to be exposed outside the class will be created inside this region.
        // Public Method naming convention :- Public Method will be exposed outside the class, 
        // The input and output parameters are also going to be exposed outside the class
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Example :
        // public bool RegisterNewCustomer(Customer Customer)
        // {
        //     bool IsCustomerDataSaved = false;
        //     //TO DO logic to save data
        //     IsCustomerDataSaved = InsertCustomerDataUsingDao(Customer);
        //     return IsCustomerDataSaved;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================
       
        #region UserManagementBus Members
        public bool SignUp(Person pPerson, User pUser, Sys_OrganizationMaster pSys_OrganizationMaster, UserOrganizationRole pUserOrganizationRole, List<UserOrganizationRole> pUserOrganizationRoles)
        {
            bool IsOperationSuccessful = false;
            //Check if Organization is insert or update?

            //check if Society is existing in SOCIETY table.
            //SocietyDao lSocietyDao = new SocietyDao();
            PersonDao lPersonDao = new PersonDao();
            UserDao lUserDao = new UserDao();
            UserOrganizationRoleDao lUserOrganizationRoleDao = new UserOrganizationRoleDao();
            //Society lSociety;
            //lSociety = lSocietyDao.GetUniqueSocietyBySocietyNameAddressLineOneCityStateCountry(pSociety);
            //If new society then create new Organization and Society
            //if (lSociety == null)
            //{
            //    Sys_OrganizationMasterDao lSys_OrganizationMasterDao = new Sys_OrganizationMasterDao();
            //    //Create new ORGANIZATION 
            //    pSys_OrganizationMaster.Sys_CreatedByID = "1"; //Since this is Signup process the user is not yet created, so defaukt creator is SystemSuperAdmin
            //    pSys_OrganizationMaster.Sys_OrganizationID = -1; //Since this is Signup process the Organization is not yet created, so defaukt creator is -1 representing the portal 
            //    if (lSys_OrganizationMasterDao.Insert(pSys_OrganizationMaster))
            //    {
            //        if (pSys_OrganizationMaster.OrganizationID > 0)
            //        {
            //            //Insert new record in society table
            //            pSociety.SocietyKey = pSys_OrganizationMaster.OrganizationID;
            //            pSociety.SocietyCode = pSys_OrganizationMaster.OrganizationCode;
            //            pSociety.Sys_CreatedByID = "1"; //Since this is Signup process the user is not yet created, so defaukt creator is SystemSuperAdmin
            //            pSociety.Sys_OrganizationID = pSys_OrganizationMaster.OrganizationID;

            //            lSocietyDao.Insert(pSociety);
            //        }
            //    }
            //}
            //Check if new record to be inseted in person table or not
            //if (pPerson.EntityState == EntityState.New)
            //{

            //    pPerson.Sys_CreatedByID = "1"; //Since this is Signup process the user is not yet created, so defaukt creater is SysyemSuperAdmn
            //    if (lSociety != null)
            //    {
            //        pPerson.Sys_OrganizationID = lSociety.SocietyKey; //For existing society
            //    }
            //    else
            //    {
            //        pPerson.Sys_OrganizationID = pSociety.SocietyKey; //For newly created society
            //    }

            //    lPersonDao.Insert(pPerson);
            //}
            //Check if new record to be insert in User table or not
            if (pUser.EntityState == EntityState.New)
            {
                pUser.UserPersonKey = pPerson.PersonKey;
                pUser.UserPassword = StringUtility.EncryptString(pUser.UserPassword);
                pUser.Sys_CreatedByID = Convert.ToString(pPerson.PersonKey);
                pUser.Sys_OrganizationID = pPerson.Sys_OrganizationID;
                lUserDao.Insert(pUser);
            }
            //Insert new records for UserOrganizationRole table

            foreach (UserOrganizationRole _UserOrganizationRole in pUserOrganizationRoles)
            {
                _UserOrganizationRole.UserKey = pUser.UserPersonKey;
                _UserOrganizationRole.UserOrganizationKey = pUser.Sys_OrganizationID;
                _UserOrganizationRole.Sys_CreatedByID = Convert.ToString(pPerson.PersonKey);
                _UserOrganizationRole.Sys_OrganizationID = pPerson.Sys_OrganizationID;
                IsOperationSuccessful = lUserOrganizationRoleDao.Insert(_UserOrganizationRole);
            }

            return IsOperationSuccessful;
        }

        public bool SignUp(Person pPerson, User pUser, List<UserOrganizationRole> pUserOrganizationRoles)
        {
            bool IsOperationSuccessful = false;

            PersonDao lPersonDao = new PersonDao();
            UserDao lUserDao = new UserDao();
            UserOrganizationRoleDao lUserOrganizationRoleDao = new UserOrganizationRoleDao();

            if (pPerson.EntityState == EntityState.New)
            {

                pPerson.Sys_CreatedByID = "1"; //Since this is Signup process the user is not yet created, so defaukt creater is SysyemSuperAdmn

                lPersonDao.Insert(pPerson);
            }
            //Check if new record to be insert in User table or not
            if (pUser.EntityState == EntityState.New)
            {
                pUser.UserPersonKey = pPerson.PersonKey;
                pUser.UserPassword = StringUtility.EncryptString(pUser.UserPassword);
                pUser.Sys_CreatedByID = Convert.ToString(pPerson.PersonKey);
                pUser.Sys_OrganizationID = pPerson.Sys_OrganizationID;
                lUserDao.Insert(pUser);
            }

            //Insert new records for UserOrganizationRole table
            foreach (UserOrganizationRole _UserOrganizationRole in pUserOrganizationRoles)
            {
                _UserOrganizationRole.UserKey = pUser.UserPersonKey;
                _UserOrganizationRole.UserOrganizationKey = -1;//This role is not society related rather global at Portal level
                _UserOrganizationRole.Sys_CreatedByID = Convert.ToString(pPerson.PersonKey);
                _UserOrganizationRole.Sys_OrganizationID = -1;//This role is not society related rather global at Portal level
                IsOperationSuccessful = lUserOrganizationRoleDao.Insert(_UserOrganizationRole);
            }

            return IsOperationSuccessful;

        }

        public bool SignUp(Person pPerson, User pUser)
        {
            bool IsOperationSuccessful = false;

            PersonDao lPersonDao = new PersonDao();
            UserDao lUserDao = new UserDao();
            UserOrganizationRoleDao lUserOrganizationRoleDao = new UserOrganizationRoleDao();

            if (pPerson.EntityState == EntityState.New)
            {

                pPerson.Sys_CreatedByID = "1"; //Since this is Signup process the user is not yet created, so defaukt creater is SysyemSuperAdmn

                if (lPersonDao.Insert(pPerson))
                {
                    //Check if new record to be insert in User table or not
                    if (pUser.EntityState == EntityState.New)
                    {
                        pUser.UserPersonKey = pPerson.PersonKey;
                        pUser.UserPassword = StringUtility.EncryptString(pUser.UserPassword);
                        pUser.Sys_CreatedByID = Convert.ToString(pPerson.PersonKey);
                        pUser.Sys_OrganizationID = pPerson.Sys_OrganizationID;
                        IsOperationSuccessful = lUserDao.Insert(pUser);
                    }
                }
            }
            
            return IsOperationSuccessful;

        }

        public bool Login(string UserLoginID, string Password)
        {
            bool IsLoginSuccess = false;
            try
            {
                UserDao lUserDao = new UserDao();
                User lUser;
                lUser = lUserDao.GetByLoginID(UserLoginID);
                if (lUser != null)
                {
                    if (StringUtility.DecryptString(lUser.UserPassword) == Password)
                    {
                        //Update session log with current UserPersonKey
                        IsLoginSuccess = true;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsLoginSuccess;
        }

        public bool IsAcitivityAuthorized(string UserOrganizationCode, string ActivityCode, string UserLoginID)
        {
            bool IsOperationSuccessful = false;

            List<RoleActivityAuthorizationReport> lRoleActivityAuthorizationReports;
            AuthorizationDao lAuthorizationDao = new AuthorizationDao();
            lRoleActivityAuthorizationReports = lAuthorizationDao.GetAllAuthorizationRoleByOrganizationAcitivityLoginId(UserOrganizationCode, ActivityCode, UserLoginID);
            if (lRoleActivityAuthorizationReports != null)
            {
                foreach (RoleActivityAuthorizationReport lTempRoleAuthorizationReport in lRoleActivityAuthorizationReports)
                {
                    IsOperationSuccessful = true;
                }
            }
            return IsOperationSuccessful;

        }

        public bool SendEmalVerificationCodeByEmailForPrimaryEmail(Person pPerson, User pUser)
        {
            bool IsOperationSuccessful = false;
            if (pPerson != null)
            {
                if (pUser != null)
                {
                    EmailUtility lEmailUtility = new EmailUtility();
                    MailMessage lMailMsg = new MailMessage();
                    lMailMsg.Subject = "FrameworkIvy - Primary verification mail";
                    lMailMsg.IsBodyHtml = true;
                    lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
                    lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style4 { font-size: large; font-weight: bold; } .style5 {font-weight:700; color:#990000;} </style> </head>";
                    lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
                    lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td><h1 class=""style1""> To verify your Primary Email ! </h1></td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3"">You have received this mail as part of our Sign-Up process</td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> Please check the following details and proceed : </td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Name : </span><span class=""style4"">" + pPerson.PersonFirstName + " " + pPerson.PersonLastName + @"</span><span class=""style3""> </span></td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Login Id : </span><span class=""style4"">" + pUser.UserLoginId + @"</span><span class=""style3""> </span></td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <span class=""style3"">Primary Email Verification Code : </span><span class=""style4"">" + pPerson.PersonPrimaryEmailVerificationCode + @"</span><span class=""style3""> </span> </td> </tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> To verify your Primary Email, please confirm with the above verification code when asked during login.</td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> FrameworkIvy Administrator </td> </tr> <tr> <td class=""style2""> &nbsp; </td> </tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2012&nbsp; <a href=""http://www.frameworkivy.com/"">www.frameworkivy.com</a> </td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

                    lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
                    lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, pPerson.PersonPrimaryEmailID);
                    lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

                    lEmailUtility.SendEmail(lMailMsg);
                    IsOperationSuccessful = true;
                }
            }
            return IsOperationSuccessful;
        }

        public bool SendNewSignUpNotificationEmail(Person pPerson, User pUser)
        {
            bool IsOperationSuccessful = false;

            EmailUtility lEmailUtility = new EmailUtility();
            MailMessage lMailMsg = new MailMessage();
            lMailMsg.Subject = "FrameworkIvy - New User Sign-Up notification";
            lMailMsg.IsBodyHtml = true;
            lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
            lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style5 { font-size: large; font-weight: bold; color: #006600; } .style6 { text-align: left; font-size: small; color: #006600; } </style> </head>";
            lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
            lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <h1 class=""style1"">New User Sign-up Notification! </h1> </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> FrameworkIvy has a User Signed-Up at the web site.</td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> User Name : <span class=""style5"">" + pPerson.PersonFirstName + " " + pPerson.PersonLastName + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> User Email : <span class=""style5"">" + pPerson.PersonPrimaryEmailID + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> User Login Id : <span class=""style5"">" + pUser.UserLoginId + "</span></td> </tr>";
            //lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> User Email : <span class=""style5"">" + SubscriberEmailId + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> SignUp Date : <span class=""style5"">" + pUser.Sys_DateOfCreation.ToString() + "</span></td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> FrameworkIvy Administrator -  Notification System </td> </tr> ";
            lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2015&nbsp; <a href=""http://www.frameworkivy.com/"">www.frameworkivy.com</a> </td></tr>";
            lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";


            lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
            lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

            lEmailUtility.SendEmail(lMailMsg);

            return IsOperationSuccessful;
        }
        #endregion

        #region Person

        public bool UpdateRegisteredPerson(Person PersonEntity)
        {
            bool IsOperationSuccessful = false;

            PersonDao lPersonDao = new PersonDao();
            IsOperationSuccessful = lPersonDao.Update(PersonEntity);

            return IsOperationSuccessful;
        }

        public bool AddPerson(Person PersonEntity)
        {
            bool IsOperationSuccessful = false;

            PersonDao lPersonDao = new PersonDao();
            IsOperationSuccessful = lPersonDao.Insert(PersonEntity);

            return IsOperationSuccessful;
        }

        public Person GetPersonByKey(int PersonKey)
        {
            PersonDao lPersonDao = new PersonDao();
            Person lPerson;
            lPerson = lPersonDao.GetByPrimaryKey(PersonKey);
            if (lPerson == null)
            {
                return null;
            }
            else
            {
                return lPerson;
            }
        }

        public Person GetPersonByPrimaryEmailId(string PersonPrimaryEmailId)
        {

            PersonDao lPersonDao = new PersonDao();
            Person lPerson;
            lPerson = lPersonDao.GetUserByEmailId(PersonPrimaryEmailId);
            if (lPerson == null)
            {
                return null;
            }
            else
            {
                return lPerson;
            }

        }

        public List<Person> GetAllUser()
        {
            List<Person> lPersonList = new List<Person>();
            PersonDao lPersonDao = new PersonDao();
            lPersonList = lPersonDao.GetAll();

            return lPersonList;
        }

        public List<PersonReport> GetAllPersonDetails()
        {
            List<PersonReport> lPersonReportList = new List<PersonReport>();
            PersonReportDao lPersonReportDao = new PersonReportDao();
            lPersonReportList = lPersonReportDao.GetAll();

            return lPersonReportList;
        }

        public PersonReport GetPersonReportByKey(int PersonKey)
        {
            PersonReportDao lPersonReportDao = new PersonReportDao();
            PersonReport lPersonReport;
            lPersonReport = lPersonReportDao.GetByKey(PersonKey);
            if (lPersonReport == null)
            {
                return null;
            }
            else
            {
                return lPersonReport;
            }
        }

        public Person GetPersonByFirstNameLastNamePrimaryEmailIdMobileNumberDateOfBirth(string FirstName, string LastName, string EmailId, string MobileNumber, Nullable<DateTime> DateOfBirth)
        {
            PersonDao lPersonDao = new PersonDao();
            Person lPerson;
            lPerson = lPersonDao.GetUniquePersonByFirstNameLastNamePrimaryEmailIdMobileNumberDateOfBirth(FirstName, LastName, EmailId, MobileNumber, DateOfBirth);
            if (lPerson == null)
            {
                return null;
            }
            else
            {
                return lPerson;
            }
        }
        public bool InsertPersonDataFromExcelSheet(List<Person> pPerson)
        {
            bool IsOperationSuccessful = false;
            foreach (Person _Person in pPerson)
            {
                PersonDao lPersonDao = new PersonDao();
                IsOperationSuccessful = lPersonDao.Insert(_Person);
            }
            return IsOperationSuccessful;
        }
        #endregion

        #region User
        //public bool SignUpNewUser(User UserEntity)
        //{
        //    bool IsOperationSuccessful = false;

        //    UserDao lUserDao = new UserDao();
        //    IsOperationSuccessful = lUserDao.Insert(UserEntity);

        //    return IsOperationSuccessful;
        //}

        public bool UpdateSignUpUser(User UserEntity)
        {
            bool IsOperationSuccessful = false;

            UserDao lUserDao = new UserDao();
            IsOperationSuccessful = lUserDao.Update(UserEntity);

            return IsOperationSuccessful;
        }

        public User GetUserByPersonKey(int UserPersonKey)
        {
            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByPrimaryKey(UserPersonKey);
            if (lUser == null)
            {
                return null;
            }
            else
            {
                return lUser;
            }
        }

        public User GetUserByLoginID(string UserLoginID)
        {
            try
            {
                UserDao lUserDao = new UserDao();
                User lUser;
                lUser = lUserDao.GetByLoginID(UserLoginID);
                if (lUser == null)
                {
                    return null;
                }
                else
                {
                    return lUser;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
        }

        public bool IsLoginIDExisting(string UserLoginID)
        {
            bool IsLoginIDExisting = true;

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByLoginID(UserLoginID);
            if (lUser == null)
            {
                IsLoginIDExisting = false;
            }

            return IsLoginIDExisting;
        }

        #endregion

        #region PersonProfileImage
        public bool SaveUserProfileImage(PersonProfileImage PersonProfileImageEntity)
        {
            bool IsOperationSuccessful = false;
            if (PersonProfileImageEntity.PersonKey > 0)
            {
                if (PersonProfileImageEntity.PersonProfileImageData != null)
                {
                    PersonProfileImageDao lPersonProfileImageDao = new PersonProfileImageDao();
                    PersonProfileImage lPersonProfileImage = lPersonProfileImageDao.GetByPrimaryKey(PersonProfileImageEntity.PersonKey);
                    if (lPersonProfileImage != null)
                    {
                        //Update existing record
                        IsOperationSuccessful = lPersonProfileImageDao.Update(PersonProfileImageEntity);
                    }
                    else
                    {
                        //Insert new record
                        IsOperationSuccessful = lPersonProfileImageDao.Insert(PersonProfileImageEntity);
                    }
                }
            }
            return IsOperationSuccessful;
        }

        public PersonProfileImage GetPersonProfileImageByPersonKey(int PersonKey)
        {
            PersonProfileImageDao lPersonProfileImageDao = new PersonProfileImageDao();
            PersonProfileImage lPersonProfileImage = lPersonProfileImageDao.GetByPrimaryKey(PersonKey);

            if (lPersonProfileImage == null)
            {
                return null;
            }
            else
            {
                return lPersonProfileImage;
            }
        }
        #endregion

        #region UserOrganizationRole
        public List<UserOrganizationRole> GetUserOrganizationRolesForSelectedUser(int UserPersonKey)
        {
            List<UserOrganizationRole> lUserOrganizationRoles;
            UserOrganizationRoleDao lUserOrganizationRoleDao = new UserOrganizationRoleDao();
            lUserOrganizationRoles = lUserOrganizationRoleDao.GetAllByUserPersonKey(UserPersonKey);
            return lUserOrganizationRoles;
        }

        public bool AddUserOrganizationRole(UserOrganizationRole UserOrganizationRoleEntity)
        {
            bool IsOperationSuccessful = false;

            UserOrganizationRoleDao lUserOrganizationRoleDao = new UserOrganizationRoleDao();
            IsOperationSuccessful = lUserOrganizationRoleDao.Insert(UserOrganizationRoleEntity);

            return IsOperationSuccessful;
        }
        public UserOrganizationRole GetByPrimaryKey(int UserKey, int UserOrganizationKey, int UserRoleKey)
        {
            UserOrganizationRoleDao _UserOrganizationRoleDao = new UserOrganizationRoleDao();
            UserOrganizationRole _UserOrganizationRole;
            _UserOrganizationRole = _UserOrganizationRoleDao.GetByPrimaryKey(UserKey, UserOrganizationKey, UserRoleKey);
            if (_UserOrganizationRole == null)
            {
                return null;
            }
            else
            {
                return _UserOrganizationRole;
            }
        }
        #endregion

        #region UserOrganizationRoleSummaryReport
        public List<UserOrganizationRoleSummaryReport> GetByUserKeyUserOrganizationKey(int UserKey, int UserOrganizationKey)
        {
            List<UserOrganizationRoleSummaryReport> lUserOrganizationRoleSummaryReportList = new List<UserOrganizationRoleSummaryReport>();
            UserOrganizationRoleDao lUserOrganizationRoleDaoDao = new UserOrganizationRoleDao();
            lUserOrganizationRoleSummaryReportList = lUserOrganizationRoleDaoDao.GetByUserKeyOrganizationKey(UserKey, UserOrganizationKey);

            return lUserOrganizationRoleSummaryReportList;
        }

        public List<Sys_RoleMaster> GetAllSysRoleMasterByUserKeyUserOrganizationKey(int UserKey, int UserOrganizationKey)
        {
            List<Sys_RoleMaster> lSys_RoleMasterList = new List<Sys_RoleMaster>();
            UserOrganizationRoleDao lUserOrganizationRoleDaoDao = new UserOrganizationRoleDao();
            lSys_RoleMasterList = lUserOrganizationRoleDaoDao.GetAllSysRoleMasterByUserKeyOrganizationKey(UserKey, UserOrganizationKey);

            return lSys_RoleMasterList;
        }
        #endregion

        #region ForgotPassword

        public bool SendVerificationCodeByEmailForForgetPassword(Person pPerson, User pUser)
        {
            bool IsOperationSuccessful = false;

            if (pPerson != null)
            {
                if (pUser != null)
                {
                    EmailUtility lEmailUtility = new EmailUtility();
                    MailMessage lMailMsg = new MailMessage();
                    lMailMsg.Subject = "FrameworkIvy - Forget password verification mail";
                    lMailMsg.IsBodyHtml = true;
                    lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
                    lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style4 { font-size: large; font-weight: bold; } </style> </head>";
                    lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'"">";
                    lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td><h1 class=""style1""> To reset your password ! </h1></td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3"">You have received this mail as part of our Forget Password process</td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> Please check the following details and proceed : </td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Name : </span><span class=""style4"">" + pPerson.PersonFirstName + " " + pPerson.PersonLastName + @"</span><span class=""style3""> </span></td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Login Id : </span><span class=""style4"">" + pUser.UserLoginId + @"</span><span class=""style3""> </span></td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <span class=""style3"">Forget Password Verification Code : </span><span class=""style4"">" + pPerson.PersonPrimaryEmailVerificationCode + @"</span><span class=""style3""> </span> </td> </tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> To reset your password, please confirm with the above verification code </td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> FrameworkIvy Administrator </td> </tr> <tr> <td class=""style2""> &nbsp; </td> </tr>";
                    //lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Follow our project at CodePlex <a href=""http://ivyframeworks.codeplex.com/""> www.ivyframeworks.codeplex.com/</a> </td> </tr>";
                    lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2012&nbsp; <a href=""http://www.frameworkivy.com/"">www.frameworkivy.com</a> </td></tr>";
                    lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

                    //lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg, "frameworkivy@gmail.com", "Administrator FrameworkIvy");
                    //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, UserEntity.UserPrimaryEmailID);
                    //lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendBcc, "frameworkivy@googlegroups.com");

                    lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg);
                    lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, pPerson.PersonPrimaryEmailID);
                    lEmailUtility.AddEmailRecepientForAdminGroupToMailMessage(lMailMsg);

                    lEmailUtility.SendEmail(lMailMsg);
                    IsOperationSuccessful = true;
                }
            }
            return IsOperationSuccessful;
        }

        public bool ResetUserPasswordByPersonKey(int PersonKey, string NewPassword)
        {
            bool IsOperationSuccessful = false;

            UserDao lUserDao = new UserDao();
            User lUser;
            lUser = lUserDao.GetByPrimaryKey(PersonKey);
            if (lUser != null)
            {
                lUser.UserPassword = StringUtility.EncryptString(NewPassword);
                lUserDao.Update(lUser);
                IsOperationSuccessful = true;
            }

            return IsOperationSuccessful;

        }

        #endregion ForgotPassword

        #region Project Member
        public bool SaveProjectMember(ProjectMemberMaster ProjectMemberMasterEntity)
        {
            bool IsOperationSuccessful = false;

            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            IsOperationSuccessful = lProjectMemberMasterDao.Insert(ProjectMemberMasterEntity);

            return IsOperationSuccessful;
        }
        public bool UpdateRegisteredProjectMember(ProjectMemberMaster ProjectMemberMasterEntity)
        {
            bool IsOperationSuccessful = false;

            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            IsOperationSuccessful = lProjectMemberMasterDao.Update(ProjectMemberMasterEntity);

            return IsOperationSuccessful;
        }
        public List<ProjectMemberMaster> GetAllProjectMember()
        {
            List<ProjectMemberMaster> lProjectMemberMasterList = new List<ProjectMemberMaster>();
            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            lProjectMemberMasterList = lProjectMemberMasterDao.GetAll();

            return lProjectMemberMasterList;
        }
        public ProjectMemberMaster GetProjectMemberByPrimaryKey(int ProjectMemberKey)
        {
            ProjectMemberMasterDao lProjectMemberMasterDao = new ProjectMemberMasterDao();
            ProjectMemberMaster lProjectMemberMaster;
            lProjectMemberMaster = lProjectMemberMasterDao.GetByPrimaryKey(ProjectMemberKey);
            if (lProjectMemberMaster == null)
            {
                return null;
            }
            else
            {
                return lProjectMemberMaster;
            }
        }
        #endregion project Member

        #endregion Public Methods

        #region Private Methods
        // All local class level methods will be inside this region. Mostly these are helper methods.
        // Private Method naming convention :- The naming should be self explanatory, 
        // make it long to explain the purpose and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Input parameter will have a "p" as prefix. 
        // This will help the parameter variable that the value is originated outside this private method.
        // Example :
        // public bool InsertCustomerDataUsingDao(Customer pCustomer)
        // {
        //     bool IsCustomerDataInsertedSuccessfully = false;
        //     //TO DO logic to save data
        //     //....
        //     //....
        //     return IsCustomerDataInsertedSuccessfully;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Private Methods
    }
}
